<?php

/**
 * Rooms View for RoomBooking Component
 * 
 * @package    Joomla.RoomBooking
 * @subpackage Components
 * @license        GNU/GPL
 */
// No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.view');

class RoomBookingViewStatistics extends JView {

    /**
     * Rooms view display method
     * @return void
     * */
    function display($tpl = null) {
        JToolBarHelper::title(JText::_('STATISTICS'), 'generic.png');
        JToolBarHelper::back('コントロールパネル', 'index.php');
//        JToolBarHelper::deleteList();
//        JToolBarHelper::editListX();
//        JToolBarHelper::addNewX();

        $model = &$this->getModel();
        
        $items = $model->getData();
        $val_arr = '';
        $mon_arr = '';
        for ($i = 0, $n = count($items); $i < $n; $i++) {
            $row = & $items[$i];
            $total = $row->cost + $row->charge;
            if($val_arr == '')$val_arr = $total;
            else $val_arr .= ','.$total;
            $ym = $row->year.'-'.$row->month;
            if($mon_arr == '')$mon_arr = $ym;
            else $mon_arr .= ','.$ym;
        }
        $this->assignRef('val', $val_arr);
        $this->assignRef('mon', $mon_arr);
        
//        echo "minhbkpro-".$val_arr;

        $db = & JFactory::getDBO();
        $query = 'SELECT id, name FROM #__users;';
        $db->setQuery($query);
        $users = $db->loadObjectList();

        foreach ($users as $user)
            $user->name = $user->id . ' - ' . $user->name;

        $defaultOpt = new stdClass();
        $defaultOpt->id = 0;
        $defaultOpt->name = '- '.JText::_( 'すべてのユーザ' ).' -';
        array_push($users, $defaultOpt);
        
        $this->assignRef('users', $users);
        
        $id_user = JRequest::getVar('id_user');if($id_user == null)$id_user = 0;
        
        $this->assignRef('id_user', $id_user);
        
        $this->assignRef('month_by_user', $this->getListMonth($model));
        $this->assignRef('year_by_user', $this->getListYear($model));
        
        $active_month = JRequest::getVar('month');
        if($active_month == null)$active_month = 0;
        
        $this->assignRef('active_month', $active_month);
        
        $active_year = JRequest::getVar('year');
        if($active_year == null)$active_year = 0;
        
        $this->assignRef('active_year', $active_year);

        parent::display($tpl);
    }
    
    function getListMonth($model){
        $items = $model->getListMonth();

        $mon_arr = '';
        for ($i = 0, $n = count($items); $i < $n; $i++) {
            $row = & $items[$i];
            $ym = $row->month;
            if($mon_arr == '')$mon_arr = $ym;
            else $mon_arr .= ','.$ym;
        }
        echo "minhbkpro-".$mon_arr;
        return $mon_arr;
    }
    
    function getListYear($model){
        $items = $model->getListYear();

        $year_arr = '';
        $year = '';
        for ($i = 0, $n = count($items); $i < $n; $i++) {
            $row = & $items[$i];
            $ym = $row->year;
            if($year != $ym){
                if($year_arr == '')$year_arr = $ym;
                else $year_arr .= ','.$ym;
            }
            $year = $ym;
        }
        
        return $year_arr;
    }

}
